package com.burstly.lib.component.networkcomponent.millennial;

import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Resources;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import com.burstly.lib.component.AbstractAdaptor;
import com.burstly.lib.component.IBurstlyAdaptor.BurstlyAdType;
import com.burstly.lib.component.IBurstlyAdaptorListener;
import com.burstly.lib.component.IBurstlyAdaptorListener.FullscreenInfo;
import com.burstly.lib.component.LayoutParametersResolver;
import com.burstly.lib.component.networkcomponent.ClickAwareWrapper;
import com.burstly.lib.util.LoggerExt;
import com.millennialmedia.android.MMAd;
import com.millennialmedia.android.MMAdView;
import com.millennialmedia.android.MMBroadcastReceiver;
import com.millennialmedia.android.MMInterstitial;
import com.millennialmedia.android.MMRequest;
import com.millennialmedia.android.MMSDK;
import java.lang.ref.Reference;
import java.lang.ref.WeakReference;
import java.util.Map;

final class MillennialAdaptor extends AbstractAdaptor
{
  static final IBurstlyAdaptorListener.FullscreenInfo FULLSCREEN_INFO = new IBurstlyAdaptorListener.FullscreenInfo("millennial", true);
  static final LoggerExt LOG = LoggerExt.getInstance();
  private static final String NETWORK_NAME = "millennial";
  private final MillennialConfigurator mConfigurator;
  MMInterstitial mInterstitialCachedView;
  boolean mIsPrecacheRequest;
  private MillennialListener mListener;
  private LocationValet mLocationValet;
  private MMAdView mView;

  MillennialAdaptor(Context paramContext, String paramString)
  {
    super(paramContext, paramString);
    this.mTag = (paramString + " MillennialAdaptor");
    this.mConfigurator = new MillennialConfigurator(this.mTag);
  }

  private void configureRequest(MMRequest paramMMRequest)
  {
    this.mConfigurator.configure(paramMMRequest);
    String str = this.mConfigurator.getGoalId();
    if (str != null)
      MMSDK.trackConversion(getContext(), str, paramMMRequest);
  }

  private MMAdView createBanner()
  {
    MMAdView localMMAdView = new MMAdView(getContext());
    localMMAdView.setApid(this.mConfigurator.getAppId());
    localMMAdView.setIgnoresDensityScaling(this.mConfigurator.isIgnoreDensity());
    localMMAdView.setId(MMSDK.getDefaultAdId());
    if ((this.mConfigurator.getWidth() != 0) && (this.mConfigurator.getHeight() != 0))
    {
      localMMAdView.setWidth(this.mConfigurator.getWidth());
      localMMAdView.setHeight(this.mConfigurator.getHeight());
    }
    return localMMAdView;
  }

  private MMInterstitial createInterstitial()
  {
    MMInterstitial localMMInterstitial = new MMInterstitial(getContext());
    localMMInterstitial.setApid(this.mConfigurator.getAppId());
    localMMInterstitial.setIgnoresDensityScaling(this.mConfigurator.isIgnoreDensity());
    return localMMInterstitial;
  }

  private void requestInterstitial(MMInterstitial paramMMInterstitial)
  {
    MMRequest localMMRequest = new MMRequest();
    configureRequest(localMMRequest);
    paramMMInterstitial.setMMRequest(localMMRequest);
    paramMMInterstitial.fetch();
  }

  private View wrap(View paramView)
  {
    ViewGroup.LayoutParams localLayoutParams;
    if ((this.mConfigurator.getWidth() == 0) || (this.mConfigurator.getHeight() == 0))
    {
      localLayoutParams = getLayoutParamsResolver().resolveParameters();
      localLayoutParams.height = -2;
    }
    while (true)
    {
      paramView.setLayoutParams(localLayoutParams);
      ClickAwareWrapper localClickAwareWrapper = new ClickAwareWrapper(getContext(), getAdaptorListener(), getViewId(), "millennial");
      localClickAwareWrapper.addView(paramView);
      return localClickAwareWrapper;
      localLayoutParams = new ViewGroup.LayoutParams((int)TypedValue.applyDimension(1, this.mConfigurator.getWidth(), getContext().getResources().getDisplayMetrics()), (int)TypedValue.applyDimension(1, this.mConfigurator.getHeight(), getContext().getResources().getDisplayMetrics()));
    }
  }

  protected void checkParameters(Map<String, ?> paramMap)
    throws IllegalArgumentException
  {
    this.mConfigurator.initConfigurator(paramMap);
    IntentFilter localIntentFilter = MMBroadcastReceiver.createIntentFilter();
    this.mListener = new MillennialListener(this, this.mTag);
    getContext().registerReceiver(this.mListener, localIntentFilter);
  }

  public void destroy()
  {
    super.destroy();
    this.mView = null;
    this.mInterstitialCachedView = null;
    if (this.mLocationValet != null)
      this.mLocationValet.stopAquire();
    this.mLocationValet = null;
  }

  protected View doGetNewAd()
  {
    this.mIsPrecacheRequest = false;
    if (this.mConfigurator.isLocationAllowed())
    {
      this.mLocationValet = new LocationValet(getContext(), this.mConfigurator);
      this.mLocationValet.startAquire(Boolean.valueOf(true));
    }
    while (this.mConfigurator.isInterstitial())
    {
      requestInterstitial(createInterstitial());
      return null;
      if (this.mLocationValet != null)
      {
        this.mLocationValet.stopAquire();
        this.mLocationValet = null;
      }
    }
    this.mView = createBanner();
    MMRequest localMMRequest = new MMRequest();
    configureRequest(localMMRequest);
    this.mView.setMMRequest(localMMRequest);
    this.mView.getAd();
    return wrap(this.mView);
  }

  public IBurstlyAdaptor.BurstlyAdType getAdType()
  {
    if (this.mConfigurator.isInterstitial())
      return IBurstlyAdaptor.BurstlyAdType.INTERSTITIAL_AD_TYPE;
    return IBurstlyAdaptor.BurstlyAdType.BANNER_AD_TYPE;
  }

  public String getNetworkName()
  {
    return "millennial";
  }

  public void pause()
  {
    super.pause();
    if (this.mLocationValet != null)
      this.mLocationValet.stopAquire();
  }

  public View precacheAd()
  {
    return getNewAd();
  }

  public void precacheInterstitialAd()
  {
    this.mIsPrecacheRequest = true;
    this.mInterstitialCachedView = createInterstitial();
    requestInterstitial(this.mInterstitialCachedView);
  }

  public void resume()
  {
    super.resume();
    if (this.mLocationValet != null)
      this.mLocationValet.startAquire(Boolean.valueOf(false));
  }

  public void showPrecachedInterstitialAd()
  {
    if (this.mInterstitialCachedView != null)
    {
      getAdaptorListener().shownFullscreen(FULLSCREEN_INFO);
      this.mInterstitialCachedView.display();
      this.mInterstitialCachedView = null;
    }
  }

  public void stop()
  {
    if (this.mListener != null)
    {
      super.stop();
      getContext().unregisterReceiver(this.mListener);
      this.mListener = null;
    }
  }

  public boolean supports(String paramString)
  {
    return true;
  }

  private static final class MillennialListener extends MMBroadcastReceiver
  {
    private final Reference<MillennialAdaptor> mAdaptor;
    private boolean mFiredDidLoad;
    private final String mLogTag;

    MillennialListener(MillennialAdaptor paramMillennialAdaptor, String paramString)
    {
      this.mAdaptor = new WeakReference(paramMillennialAdaptor);
      this.mLogTag = paramString;
    }

    public void displayStarted(MMAd paramMMAd)
    {
      super.displayStarted(paramMMAd);
    }

    public void fetchFailure(MMAd paramMMAd)
    {
      super.fetchFailure(paramMMAd);
      MillennialAdaptor localMillennialAdaptor = (MillennialAdaptor)this.mAdaptor.get();
      if ((localMillennialAdaptor != null) && (!localMillennialAdaptor.isStopped()))
      {
        MillennialAdaptor.LOG.logInfo(this.mLogTag, "Failed to fetch Millennial...", new Object[0]);
        localMillennialAdaptor.stop();
        localMillennialAdaptor.getAdaptorListener().failedToLoad("millennial", paramMMAd instanceof MMInterstitial, "Failed to fetch");
      }
    }

    public void fetchFinishedCaching(MMAd paramMMAd)
    {
      super.fetchFinishedCaching(paramMMAd);
      MillennialAdaptor localMillennialAdaptor = (MillennialAdaptor)this.mAdaptor.get();
      MMAd localMMAd;
      MMInterstitial localMMInterstitial;
      IBurstlyAdaptorListener localIBurstlyAdaptorListener;
      if ((localMillennialAdaptor != null) && (!localMillennialAdaptor.isStopped()))
      {
        MillennialAdaptor.LOG.logInfo(this.mLogTag, "Fetch finished caching Millennial...", new Object[0]);
        if (!(paramMMAd instanceof MMInterstitial))
          break label108;
        localMMAd = paramMMAd;
        localMMInterstitial = (MMInterstitial)localMMAd;
        localIBurstlyAdaptorListener = localMillennialAdaptor.getAdaptorListener();
        if (localMMInterstitial == null)
          break label114;
      }
      label108: label114: for (boolean bool = true; ; bool = false)
      {
        localIBurstlyAdaptorListener.didLoad("millennial", bool);
        if (localMMInterstitial != null)
        {
          if (!localMillennialAdaptor.mIsPrecacheRequest)
            break label120;
          localMillennialAdaptor.mInterstitialCachedView = localMMInterstitial;
        }
        return;
        localMMAd = null;
        break;
      }
      label120: localMillennialAdaptor.getAdaptorListener().shownFullscreen(MillennialAdaptor.FULLSCREEN_INFO);
      localMMInterstitial.display();
    }

    public void fetchStartedCaching(MMAd paramMMAd)
    {
      super.fetchStartedCaching(paramMMAd);
    }

    public void getAdFailure(MMAd paramMMAd)
    {
      super.getAdFailure(paramMMAd);
      MillennialAdaptor localMillennialAdaptor = (MillennialAdaptor)this.mAdaptor.get();
      if ((localMillennialAdaptor != null) && (!localMillennialAdaptor.isStopped()))
      {
        MillennialAdaptor.LOG.logInfo(this.mLogTag, "Failed to receive Millennial...", new Object[0]);
        localMillennialAdaptor.stop();
        localMillennialAdaptor.getAdaptorListener().failedToLoad("millennial", paramMMAd instanceof MMInterstitial, "");
      }
    }

    public void getAdSuccess(MMAd paramMMAd)
    {
      super.getAdSuccess(paramMMAd);
      if (this.mFiredDidLoad);
      MillennialAdaptor localMillennialAdaptor;
      do
      {
        return;
        this.mFiredDidLoad = true;
        localMillennialAdaptor = (MillennialAdaptor)this.mAdaptor.get();
      }
      while ((localMillennialAdaptor == null) || (localMillennialAdaptor.isStopped()));
      localMillennialAdaptor.getAdaptorListener().didLoad("millennial", paramMMAd instanceof MMInterstitial);
    }

    public void onReceive(Context paramContext, Intent paramIntent)
    {
      super.onReceive(paramContext, paramIntent);
      MillennialAdaptor localMillennialAdaptor = (MillennialAdaptor)this.mAdaptor.get();
      if ((localMillennialAdaptor != null) && (!localMillennialAdaptor.isStopped()))
        MillennialAdaptor.LOG.logInfo(this.mLogTag, "Receive Millennial ad...", new Object[0]);
    }

    public void overlayClosed(MMAd paramMMAd)
    {
      super.overlayClosed(paramMMAd);
      MillennialAdaptor localMillennialAdaptor = (MillennialAdaptor)this.mAdaptor.get();
      if ((localMillennialAdaptor != null) && (!localMillennialAdaptor.isStopped()))
      {
        localMillennialAdaptor.getAdaptorListener().dismissedFullscreen(MillennialAdaptor.FULLSCREEN_INFO);
        localMillennialAdaptor.stop();
      }
    }

    public void overlayOpened(MMAd paramMMAd)
    {
      super.overlayOpened(paramMMAd);
    }

    public void overlayTap(MMAd paramMMAd)
    {
      super.overlayTap(paramMMAd);
    }
  }
}

/* Location:           E:\android\Toy Defense 2_dex2jar.jar
 * Qualified Name:     com.burstly.lib.component.networkcomponent.millennial.MillennialAdaptor
 * JD-Core Version:    0.6.2
 */